import Vue from 'vue'
import Vuex from 'vuex'
import { findMatchPermissionItem } from '@/config/permission'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    menuList: [],
    menuIndexData: {},
    breadCrunbData: {},
    shoporderData: {},
    shopdetailData: {},
    permissionData: []
  },
  getters: {
    secondMenuList: state => {
      if (state.menuIndexData.pIndex == -1) {
        return []
      } else {
        try {
          return state.menuList[state.menuIndexData.pIndex].child
        } catch (error) {
          return []
        }
      }
    },
    navNum: state => {
      let arr = []
      let num = 0
      if (state.menuIndexData.pIndex == -1) {
        arr = []
      } else {
        try {
          arr = state.menuList[state.menuIndexData.pIndex].child
        } catch (error) {
          arr = []
        }
      }
      arr.map(obj => {
        if (obj.hideInMenu) {
          obj.child &&
            obj.child.map(item => {
              !item.hideInMenu && num++
            })
        } else {
          num++
        }
      })
      return num
    },
    shoporderData: state => {
      return state.shoporderData
    },
    shopdetailData: state => {
      return state.shopdetailData
    },
    permissionData: state => {
      return state.permissionData
    }
  },
  mutations: {
    setMenuIndex(state, data) {
      state.menuIndexData = data
    },
    setMenu(state, list) {
      state.menuList = list
    },
    setBreakData(state, data) {
      state.breadCrunbData = data
    },
    setShopOrder(state, data) {
      state.shoporderData = data
    },
    setShopDetail(state, data) {
      state.shopdetailData = data
    },
    setPermissionData(state, data) {
      // 数据简报、账户信息默认有权限
      // const obj1 = findMatchPermissionItem(item => item.id === 1, data)
      // obj1.is_bind = true
      // const obj2 = findMatchPermissionItem(item => item.id === 5, data)
      // obj2.is_bind = true
      // const obj3 = findMatchPermissionItem(item => item.id === 503, data)
      // obj3.is_bind = true

      state.permissionData = data
    }
  },
  actions: {
    setMenuList(context, list) {
      context.commit('setMenu', list)
    },
    setMenuIndex(context, data) {
      context.commit('setMenuIndex', data)
    },
    setBreak(context, data) {
      context.commit('setBreakData', data)
    },

    setShop(context, data) {
      context.commit('setShopOrder', data)
    },
    setDetail(context, data) {
      context.commit('setShopDetail', data)
    },
    setPermissionData(context, data) {
      context.commit('setPermissionData', data)
    }
  },
  modules: {}
})
